package com.ufgov.agcfs.baseData.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.SearchFilter;

import com.ufgov.agcfs.baseData.dao.AssetTypeDao;
import com.ufgov.agcfs.baseData.entity.AssetType;
import com.ufgov.agcfs.pub.UUID;

@Service
public class AssetTypeService {
	@Autowired
	public AssetTypeDao assetTypeDao;
	
	public List<AssetType> getAssetList(){
		return assetTypeDao.getAssetList();
	}
	
	public List<AssetType> findAll(){
		AssetType assetType = new AssetType();		
		return assetTypeDao.findAllAssetType(assetType);
	}
	
	
	public void updateAssetType(AssetType assetType){
		assetTypeDao.updateAssetType(assetType);
	}
	
	public  List<AssetType> findSearch(
			Map<String, Object> searchParams, Sort sort) {
		
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		SearchFilter searchFilterBynd =filters.get("EQ_nd");
		String nd = "";
		if (searchFilterBynd!=null) {
			nd = (String) searchFilterBynd.value;
		}
		List<AssetType> assetTypeDetailList;
		if (nd == ""){
			AssetType assetType = new AssetType();	
			assetTypeDetailList= assetTypeDao.findAllAssetType(assetType);
		}
		else{
			assetTypeDetailList= assetTypeDao.findSearchAssetType(nd);
		}

		return assetTypeDetailList;
	}
	
	public AssetType findByNdCode(String nd,String assetTypeCode){
		AssetType assetType = new AssetType();
		assetType.setNd(nd);
		assetType.setAsassetTypeCode(assetTypeCode);
		List<AssetType> assetTypeList = assetTypeDao.findAllAssetType(assetType);
		if (assetTypeList.size()==0){
			return null;
		} else {
			return (AssetType)assetTypeList.get(0);
		}
	}
	
	public List<String> getAllNd(){
//		return assetTypeDao.getAllNd();
		Date nowTime=new Date(); 
		SimpleDateFormat dataFmt=new SimpleDateFormat("yyyy"); 
		String curYear = dataFmt.format(nowTime); 
		List<String> ndList = new ArrayList<String>();
		for (int i = 0; i < 5; i++) {
			ndList.add(String.valueOf(Integer.parseInt(curYear)+i));
		}
		return ndList;
	}	
	
	@Transactional
	public void delAssetType(String id,
			Map<String, String> resultMap) {
		// TODO 自动生成的方法存根
//		if(ndId!="" && ndId!=null){
//			assetTypeDao.delete(ndId);
//		}
//		
//		List<AssetType> assetTypeDetailList =assetTypeDao.findIpDictionaryDetailByDicId(dicId);
//		if(assetTypeDetailList.size()==0){
		AssetType asset = new AssetType();
		asset.setId(id);		
		assetTypeDao.delAssetType(asset);
//		}
			
	}	
	
	@Transactional
	public void saveAssetType(AssetType assetType,
			Map<String, String> resultMap) {

		if ((assetType.getId().equals(""))||(assetType.getId()==null)) {
			AssetType assetTypeNew = findByNdCode(assetType.getNd(),assetType.getAssetTypeCode());
			if (assetTypeNew!=null){
				getErrorMsg(resultMap,"saveError","系统中已存在相同科目！");
				return;
			}
			assetType.setId(UUID.randomUUID().toString());
			assetTypeDao.insertAssetType(assetType);
		} else {
			assetTypeDao.updateAssetType(assetType);
		}		
								
	}
	
	private void getErrorMsg(Map<String, String> resultMap,String flg, String reason) {
		resultMap.put("result", "fail");
		resultMap.put(flg, reason);
		resultMap.put("reason", "保存失败！");
	}	
	
}
